package com.example.studyllm.service.rag;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.studyllm.entity.rag.RagSession;
import com.example.studyllm.mapper.rag.RagSessionMapper;
import com.example.studyllm.service.base.BaseServiceImpl;
import com.example.studyllm.utils.BeanUtils;
import com.example.studyllm.utils.UserUtils;
import com.example.studyllm.vo.base.PageResultVO;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 *   service实现类
 * </p>
 *
 * @author kw
 * @since 2025-05-20 09:33:14.822775
 */
@Service
public class RagSessionService extends BaseServiceImpl<RagSessionMapper, RagSession> {


    public RagSession addRagSession(RagSession ragSession) {
        ragSession.setUserId(UserUtils.getUserId());
        ragSession.setCreateTime(LocalDateTime.now());
        ragSession.setSessionId(UUID.randomUUID().toString());
        this.save(ragSession);
        return ragSession;
    }

    public Boolean updateRagSessionById(RagSession ragSession) {
        return this.updateById(ragSession);
    }

    public PageResultVO<RagSession> getPage(RagSession pageParam) {

        LambdaQueryWrapper<RagSession> lambda = new QueryWrapper<RagSession>().lambda();
        this.buildCondition(lambda, pageParam);
        lambda.orderBy(true, false, RagSession::getId);     
        PageResultVO<RagSession> resultPage = BeanUtils.changePage(RagSession.class, this.page(new Page<>(pageParam.getPageNo(), pageParam.getPageSize()), lambda));
        return resultPage;
    }
    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RagSession> lambda, RagSession param) {
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getId()),RagSession::getId, param.getId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getSessionId()),RagSession::getSessionId, param.getSessionId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getUserId()),RagSession::getUserId, param.getUserId());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getCreateTime()),RagSession::getCreateTime, param.getCreateTime());
        
            lambda.eq(ObjectUtil.isNotEmpty(param.getDescr()),RagSession::getDescr, param.getDescr());
        
    }
}